home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / gnused.zip / GNUSED / GETOPT1.C < prev    next >
C/C++ Source or Header  |  1992-11-06  |  4KB  |  159 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "getopt.h"
  19.  
  20. #ifndef __STDC__
  21. #define const
  22. #endif
  23.  
  24. #if !defined (NULL)
  25. #define NULL 0
  26. #endif
  27.  
  28. int
  29. getopt_long (argc, argv, options, long_options, opt_index)
  30.      int argc;
  31.      char **argv;
  32.      const char *options;
  33.      const struct option *long_options;
  34.      int *opt_index;
  35. {
  36.   int val;
  37.  
  38.   _getopt_long_options = long_options;
  39.   val = getopt (argc, argv, options);
  40.   if (val == 0 && opt_index != NULL)
  41.     *opt_index = option_index;
  42.   return val;
  43. }
  44.  
  45. /* Like getopt_long, but '-' as well as '+' can indicate a long option.
  46.    If an option that starts with '-' doesn't match a long option,
  47.    but does match a short option, it is parsed as a short option
  48.    instead. */
  49.  
  50. int 
  51. getopt_long_only (argc, argv, options, long_options, opt_index)
  52.      int argc;
  53.      char **argv;
  54.      const char *options;
  55.      const struct option *long_options;
  56.      int *opt_index;
  57. {
  58.   int val;
  59.  
  60.   _getopt_long_options = long_options;
  61.   _getopt_long_only = 1;
  62.   val = getopt (argc, argv, options);
  63.   if (val == 0 && opt_index != NULL)
  64.     *opt_index = option_index;
  65.   return val;
  66. }
  67.  
  68.  
  69. #ifdef TEST
  70.  
  71. #include <stdio.h>
  72.  
  73. int
  74. main (argc, argv)
  75.      int argc;
  76.      char **argv;
  77. {
  78.   int c;
  79.   int digit_optind = 0;
  80.  
  81.   while (1)
  82.     {
  83.       int this_option_optind = optind ? optind : 1;
  84.       char *name = '\0';
  85.       int option_index = 0;
  86.       static struct option long_options[] =
  87.       {
  88.     {"add", 1, 0, 0},
  89.     {"append", 0, 0, 0},
  90.     {"delete", 1, 0, 0},
  91.     {"verbose", 0, 0, 0},
  92.     {"create", 0, 0, 0},
  93.     {"file", 1, 0, 0},
  94.     {0, 0, 0, 0}
  95.       };
  96.  
  97.       c = getopt_long (argc, argv, "abc:d:0123456789",
  98.                long_options, &option_index);
  99.       if (c == EOF)
  100.     break;
  101.  
  102.       switch (c)
  103.     {
  104.     case 0:
  105.       printf ("option %s", (long_options[option_index]).name);
  106.       if (optarg)
  107.         printf (" with arg %s", optarg);
  108.       printf ("\n");
  109.       break;
  110.  
  111.     case '0':
  112.     case '1':
  113.     case '2':
  114.     case '3':
  115.     case '4':
  116.     case '5':
  117.     case '6':
  118.     case '7':
  119.     case '8':
  120.     case '9':
  121.       if (digit_optind != 0 && digit_optind != this_option_optind)
  122.         printf ("digits occur in two different argv-elements.\n");
  123.       digit_optind = this_option_optind;
  124.       printf ("option %c\n", c);
  125.       break;
  126.  
  127.     case 'a':
  128.       printf ("option a\n");
  129.       break;
  130.  
  131.     case 'b':
  132.       printf ("option b\n");
  133.       break;
  134.  
  135.     case 'c':
  136.       printf ("option c with value `%s'\n", optarg);
  137.       break;
  138.  
  139.     case '?':
  140.       break;
  141.  
  142.     default:
  143.       printf ("?? getopt returned character code 0%o ??\n", c);
  144.     }
  145.     }
  146.  
  147.   if (optind < argc)
  148.     {
  149.       printf ("non-option ARGV-elements: ");
  150.       while (optind < argc)
  151.     printf ("%s ", argv[optind++]);
  152.       printf ("\n");
  153.     }
  154.  
  155.   exit (0);
  156. }
  157.  
  158. #endif /* TEST */
  159.